package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import org.springframework.stereotype.Repository;
import com.aidong.model.wills.*;

import java.util.Date;
import java.util.List;

@Repository
public class CardTypeCodeDao extends BaseDao<CardTypeCode, String> {

	public List<CardTypeCode> getShopCardTypeList(String shopCode, int saleType, int option) {
		String sql = "select distinct ct.code, ct.chName, ct.price*discount price\n" +
				"from CardTypeCode ct inner join CardShopLevel cl on ct.code=cl.CardTypeCode\n" +
				"inner join DicShopCode s on cl.LevelCode=s.LevelCode\n" +
				"where s.code=:p1 and barcodeUse=0 and ct.ActiveTime<getdate() and ct.InactiveTime>getdate()";
		if(saleType>=1 && saleType<=3) sql += " and SaleType"+saleType+"=1";
		if(saleType==3 && (option & 1)==0) sql += " and preferRenewal<>1";
		if((option & 2)==0)
			sql += " and (promotionCard is null or promotionCard<>1)";
		else
			sql += " and promotionCard=1";
		sql += " order by ct.code";
		return queryListBySql(CardTypeCode.class, sql, shopCode);
	}

	public List<CardTypeCode> getValidCardList() {
		return queryList("ActiveTime<getdate() and InactiveTime>getdate()", null, null, -1);
	}

	public List<CardTypeCode> getPromotionCardList() {
		return queryList("PromotionCard=1", null, null, -1);
	}

	public List<CardTypeCode> getMakeCardList() {
		return queryList("markId<>''", null, new OrderSet().asc("markId"), -1);
	}

	public Double getCardTypeDiscount(String cardType, String shopCode) {
		String sql = "select discount from CardShopLevel cl inner join DicShopCode s on cl.LevelCode=s.LevelCode\n"
				+ "where cl.cardTypeCode=:p1 and s.code=:p2";
		return getBySql(Double.class, sql, cardType, shopCode);
	}

	public void addCardType(CardTypeCode cardType, List<CardTimeLimit> timeList, List<CardShopLevel> levelList,
							List<CardQuantity> quanList, List<CardManyUser> userList) {
		add(cardType);
		saveExtList(cardType, timeList, levelList, quanList, userList);
	}

	public void updateCardType(CardTypeCode cardType, List<CardTimeLimit> timeList, List<CardShopLevel> levelList,
							List<CardQuantity> quanList, List<CardManyUser> userList) {
		update(cardType);
		saveExtList(cardType, timeList, levelList, quanList, userList);
	}

	private void saveExtList(CardTypeCode cardType, List<CardTimeLimit> timeList, List<CardShopLevel> levelList,
						List<CardQuantity> quanList, List<CardManyUser> userList) {
		for(CardTimeLimit timeLimit: timeList) {
			if("-".equals(timeLimit.getCardTypeCode())) {
				CardTimeLimit timeLimit1 = (CardTimeLimit)getSession().get(CardTimeLimit.class, timeLimit.getAutoId());
				if(timeLimit1!=null) getSession().delete(timeLimit1);
			} else {
				timeLimit.setCardTypeCode(cardType.getCode());
				getSession().saveOrUpdate(timeLimit);
			}
		}
		for(CardShopLevel shopLevel: levelList) {
			if("-".equals(shopLevel.getCardTypeCode())) {
				CardShopLevel shopLevel1 = (CardShopLevel)getSession().get(CardShopLevel.class, shopLevel.getAutoId());
				getSession().delete(shopLevel1);
			} else {
				shopLevel.setCardTypeCode(cardType.getCode());
				getSession().saveOrUpdate(shopLevel);
			}
		}
		for(CardQuantity quantity: quanList) {
			if("-".equals(quantity.getCardTypeCode())) {
				CardQuantity quantity1 = (CardQuantity)getSession().get(CardQuantity.class, quantity.getAutoId());
				getSession().delete(quantity1);
			} else {
				quantity.setCardTypeCode(cardType.getCode());
				getSession().saveOrUpdate(quantity);
			}
		}
		for(CardManyUser manyUser: userList) {
			if("-".equals(manyUser.getCardTypeCode())) {
				CardManyUser manyUser1 = (CardManyUser)getSession().get(CardManyUser.class, manyUser.getAutoId());
				getSession().delete(manyUser1);
			} else {
				manyUser.setCardTypeCode(cardType.getCode());
				getSession().saveOrUpdate(manyUser);
			}
		}
	}

}